﻿Imports System.Runtime.InteropServices

''' <summary>
''' Provides methods for manipulating the Blur applied to a given window.
''' </summary>
Friend Class BlurBehind
    Private m_rcBlur As AeroAPIs.RECT
    Private m_hwnd As IntPtr
    Private m_hRgn As IntPtr
    Private m_fEnabled As Boolean
    Private m_fTrans As Boolean

#If WPF Then
		Private m_wnd As Visual
#End If

    ''' <summary>
    ''' Initializes a new instance of AeroApis.Dwm_BlurBehindAttributes.
    ''' </summary>
    ''' <param name="hwnd">A handle to the window that is to be manipuated.</param>
    Sub New(ByVal hwnd As IntPtr)
        If hwnd = IntPtr.Zero Then
            Throw New ArgumentNullException("hwnd")
        End If

        m_hwnd = hwnd
    End Sub

    ''' <summary>
    ''' Initializes a new instance of AeroApis.Dwm_BlurBehindAttributes.
    ''' </summary>
    ''' <param name="form">The <see cref="System.Windows.Forms.Form"/> that is to be manipuated.</param>
    Friend Sub New(ByVal form As Form)
        If form Is Nothing Then
            Throw New ArgumentNullException("form")
        End If

        m_hwnd = form.Handle
    End Sub

    ''' <summary>
    ''' Gets the handle to the window which is associated to this instance.
    ''' </summary>
    Public ReadOnly Property Handle() As IntPtr
        Get
            Return m_hwnd
        End Get
    End Property

    ''' <summary>
    ''' Gets or sets whether Blur is applied to the given window.
    ''' </summary>
    Public Property Enabled() As Boolean
        Get
            Return m_fEnabled
        End Get
        Set(ByVal value As Boolean)
            If Not AeroEnabled Then
                Return
            End If

            Dim bb As New AeroAPIs.DWM_BlurBehind()
            bb.dwFlags = BlurBehindFlags.Enable
            bb.fEnable = value
            AeroAPIs.DwmEnableBlurBehindWindow(m_hwnd, bb)
            m_fEnabled = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the area, as a rectangle, used to apply Blur in the given window.
    ''' </summary>
    Public Property RectRegion() As AeroAPIs.RECT
        Get
            If m_hRgn = IntPtr.Zero Then
                GetClientRect(m_hwnd, m_rcBlur)
            End If

            Return m_rcBlur
        End Get
        Set(ByVal value As AeroAPIs.RECT)
            HRegion = CreateRectRgn(value.Left, value.Top, value.Right, value.Bottom)

            m_rcBlur = value
        End Set
    End Property
    <DllImport("Gdi32")> _
    Public Shared Function CreateRectRgn(ByVal nLeftRect As Integer, ByVal nTopRect As Integer, ByVal nRightRect As Integer, ByVal nBottomRect As Integer) As IntPtr
    End Function
    <DllImport("User32")> _
    Public Shared Function GetClientRect(ByVal hWnd As IntPtr, ByVal lpRect As AeroAPIs.RECT) As Integer
    End Function
    ''' <summary>
    ''' Gets or sets a handle to the region used to apply Blur in the given window.
    ''' </summary>
    Public Property HRegion() As IntPtr
        Get
            Return m_hRgn
        End Get
        Set(ByVal value As IntPtr)
            If Not AeroEnabled Then
                Return
            End If

            Dim bb As New AeroAPIs.DWM_BlurBehind()
            bb.dwFlags = BlurBehindFlags.Enable Or BlurBehindFlags.BlurRegion
            bb.fEnable = True
            bb.hRgnBlur = value
            AeroAPIs.DwmEnableBlurBehindWindow(m_hwnd, bb)

            m_hRgn = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets whether a black background will be drawn when a maximized window is present.
    ''' </summary>
    Public Property TransitionOnMaximized() As Boolean
        Get
            Return m_fTrans
        End Get
        Set(ByVal value As Boolean)
            If Not AeroEnabled Then
                Return
            End If
            Dim bb As New AeroAPIs.DWM_BlurBehind()
            bb.dwFlags = BlurBehindFlags.TransitionOnMaximized
            bb.fTransitionOnMaximized = value
            AeroAPIs.DwmEnableBlurBehindWindow(m_hwnd, bb)

            m_fTrans = value
        End Set
    End Property
    Friend ReadOnly Property AeroEnabled As Boolean
        Get
            Return HyperAero.WindowsAeroEnabled
        End Get
    End Property
    ''' <summary>
    ''' Returns a textual representation of the current instance.
    ''' </summary>
    ''' <returns>A textual representation of the current instance.</returns>
    Public Overrides Function ToString() As String
        Return String.Format(System.Globalization.CultureInfo.InvariantCulture, "{{Enabled = {0}, Region = 0x{1:X}}}", Enabled, HRegion)
    End Function

End Class
''' <summary>
''' Used in blur behind data structures
''' </summary>
<Flags()> _
Public Enum BlurBehindFlags As Integer
    ''' <summary>
    ''' fEnable has been specified
    ''' </summary>
    Enable = 1
    ''' <summary>
    ''' hRgnBlur has been specified
    ''' </summary>
    BlurRegion = 2
    ''' <summary>
    ''' fTransitionOnMaximized has been specified
    ''' </summary>
    TransitionOnMaximized = 4
End Enum